package com.example.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/user1")
@RestController

public class UserController1 {
    @RequestMapping(value = "/m1",method = RequestMethod.GET)

    public String hellouse1(){
        return "hello user1";
    }

    @RequestMapping(value = "/m2",method = RequestMethod.POST)

    public String hellouse2(){
        return "hello user2";
    }

  @GetMapping("/m3")

    public String hellouse3(){
        return "hello user3";
    }

    @PostMapping("/m4")

    public String hellouse4(){
        return "hello user4";
    }


    @RequestMapping("/m5")
    public String hello(String kewword){
        return "hello"+kewword;
    }

    @RequestMapping("/m6")
    public String SignIn(String name,String keyword){
        return "用户名："+name+"   ,密码："+keyword;
    }


    @GetMapping("/m7")
    public String m7(){
        return "m7";
    }
    @PostMapping("/m7")
public String m8(){
        return "m8";
    }

    @RequestMapping("/m8")
    public String m8(int val){
        return ""+val;
    }

    @RequestMapping("/m9")
    public String m8(Integer val){
        return ""+val;
    }

    @RequestMapping("/m10")
    public String m10(UserInfo userInfo){
        return "接收参数：userInfo="+userInfo.toString();
    }

    @RequestMapping("m11")
    public String m11(@RequestParam(value = "q",required = false) String keyword){
        return "接收参数"+keyword;
    }

    @RequestMapping("/m12")
    public String m12(String []arr){
        return "arr"+ Arrays.toString(arr);
    }

    @RequestMapping("/m13")
public String m13(@RequestParam List<Integer>list){
        return list.toString();
    }

    @RequestMapping("/m14")
    public String m14(@RequestBody UserInfo userInfo){
        System.out.println(userInfo.getName());
      return  userInfo.toString();
    }

    @RequestMapping("/article/{type}/{articleId}")
    public String m15(@PathVariable Integer articleId,@PathVariable String type){
return "获取文章类型："+type+"获取文章ID"+articleId;
    }

    @RequestMapping("/article/{type}")
    public String m15(@PathVariable String type){
        return "获取文章类型："+type;
    }

    @RequestMapping("/m16")
    public String m16(@RequestParam("file11") MultipartFile file) throws IOException {
        System.out.println(file.getName());
        file.transferTo(new File("D:\\图片\\代码测试图片\\"+file.getOriginalFilename()));
        return "文件上传成功";
    }

    @RequestMapping("/m17")
    public String m17(HttpServletRequest request){
        Cookie[]cookies= request.getCookies();
        if(cookies!=null){
            for (Cookie cookie:cookies) {
                System.out.println(cookie.getName()+":"+cookie.getValue());
            }
        }
        return "返回Cookie成功";
    }

    @RequestMapping("/m18")
public String m18(@CookieValue("name")String name){
        return  "从Cookie中获取name的值："+name;
    }

@RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        //false为null，true为一个空的session对象
    HttpSession session=request.getSession();
        session.setAttribute("userName","zhangsan");
session.setAttribute("age",17);
return "设置session成功";
}


@RequestMapping("/getSession1")
public String getSession(HttpServletRequest request) {
    HttpSession session = request.getSession(false);
    if (session == null) {
        return "未登录";
    } else {
        String userName = (String) session.getAttribute("userName");
        return "登录用户为：" + userName;
    }

}
    @RequestMapping("/getSession2")
    public String getSession2 (HttpSession session){
        if (session == null) {
            return "未登录";
        } else {
            String userName = (String) session.getAttribute("userName");
            return "登录用户为：" + userName;
        }
    }

    @RequestMapping("/getSession3")
    public String getSession3 (@SessionAttribute("userName") String userName){
        return "登录用户为：" + userName;
    }

    @RequestMapping("/getHeader")
    public String getHeader (HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "从header中获取userAgent：" + userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2 (@RequestHeader("User-Agent") String userAgent){
        return "从header中获取userAgent：" + userAgent;
    }
}
